import java.io.*;

public class GUI extends javax.swing.JFrame {

	
	private static final long serialVersionUID = 1L;
		public GUI() {
	        initComponents();
	    }

	    private void initComponents() {

	        buttonGroup1 = new javax.swing.ButtonGroup();
	        masterUI = new javax.swing.JTabbedPane();
	        homeFace = new javax.swing.JPanel();
	        homelbl = new javax.swing.JLabel();
	        numCPUlbl = new javax.swing.JLabel();
	        runButton = new javax.swing.JButton();
	        cpunumspin = new javax.swing.JSpinner();
	        scheduleFace = new javax.swing.JPanel();
	        schedulelbl = new javax.swing.JLabel();
	        FCFSbutton = new javax.swing.JRadioButton();
	        prioritybutton = new javax.swing.JRadioButton();
	        SJFbutton = new javax.swing.JRadioButton();
	        jobsFace = new javax.swing.JPanel();
	        jobslbl = new javax.swing.JLabel();
	        jobstxt = new javax.swing.JTextField();
	        pagingFace = new javax.swing.JPanel();
	        paginglbl = new javax.swing.JLabel();
	        pagingchk = new javax.swing.JCheckBox();
	        jScrollPane1 = new javax.swing.JScrollPane();
	        OSTextArea = new javax.swing.JTextArea();

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	        setTitle("WiMi Java OS");

	        masterUI.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
	        masterUI.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
	        masterUI.setName("menubar"); // NOI18N

	        homelbl.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
	        homelbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	        homelbl.setText("Welcome to the WiMi virtual machine");
	        homelbl.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

	        numCPUlbl.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
	        numCPUlbl.setText("Number of CPUs");

	        runButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
	        runButton.setText("Run OS");
	        runButton.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                try {
						runButtonActionPerformed(evt);
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }
	        });

	        cpunumspin.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
	        cpunumspin.setModel(new javax.swing.SpinnerNumberModel(4, 1, 10, 1));

	        javax.swing.GroupLayout homeFaceLayout = new javax.swing.GroupLayout(homeFace);
	        homeFace.setLayout(homeFaceLayout);
	        homeFaceLayout.setHorizontalGroup(
	            homeFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(homeFaceLayout.createSequentialGroup()
	                .addGap(57, 57, 57)
	                .addComponent(numCPUlbl, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
	                .addComponent(cpunumspin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(49, 49, 49)
	                .addComponent(runButton)
	                .addGap(38, 38, 38))
	            .addGroup(homeFaceLayout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(homelbl, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
	                .addContainerGap())
	        );
	        homeFaceLayout.setVerticalGroup(
	            homeFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(homeFaceLayout.createSequentialGroup()
	                .addGap(24, 24, 24)
	                .addComponent(homelbl, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(32, 32, 32)
	                .addGroup(homeFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(numCPUlbl, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(cpunumspin, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(runButton))
	                .addContainerGap(30, Short.MAX_VALUE))
	        );

	        masterUI.addTab("<html><body marginwidth=23 marginheight=4>Home</body></html>", homeFace);

	        schedulelbl.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
	        schedulelbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	        schedulelbl.setText("Select your Scheduling Algorithm");

	        buttonGroup1.add(FCFSbutton);
	        FCFSbutton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
	        FCFSbutton.setText("FCFS");
	        FCFSbutton.setToolTipText("First Come First Serve");

	        buttonGroup1.add(prioritybutton);
	        prioritybutton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
	        prioritybutton.setSelected(true);
	        prioritybutton.setText("PRIORITY");

	        buttonGroup1.add(SJFbutton);
	        SJFbutton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
	        SJFbutton.setText("SJF");
	        SJFbutton.setToolTipText("Shortest Job First");

	        javax.swing.GroupLayout scheduleFaceLayout = new javax.swing.GroupLayout(scheduleFace);
	        scheduleFace.setLayout(scheduleFaceLayout);
	        scheduleFaceLayout.setHorizontalGroup(
	            scheduleFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(scheduleFaceLayout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(schedulelbl, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
	                .addContainerGap())
	            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, scheduleFaceLayout.createSequentialGroup()
	                .addGap(63, 63, 63)
	                .addComponent(FCFSbutton)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
	                .addComponent(prioritybutton)
	                .addGap(65, 65, 65)
	                .addComponent(SJFbutton)
	                .addGap(63, 63, 63))
	        );
	        scheduleFaceLayout.setVerticalGroup(
	            scheduleFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(scheduleFaceLayout.createSequentialGroup()
	                .addGap(32, 32, 32)
	                .addComponent(schedulelbl, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(18, 18, 18)
	                .addGroup(scheduleFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(SJFbutton)
	                    .addComponent(prioritybutton)
	                    .addComponent(FCFSbutton))
	                .addContainerGap(46, Short.MAX_VALUE))
	        );

	        masterUI.addTab("<html><body marginwidth=15 marginheight=4>Scheduling</body></html>", scheduleFace);

	        jobslbl.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
	        jobslbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	        jobslbl.setText("Select your Job file");

	        jobstxt.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
	        jobstxt.setForeground(new java.awt.Color(102, 102, 102));
	        jobstxt.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
	        jobstxt.setText("DataFile2.txt");

	        javax.swing.GroupLayout jobsFaceLayout = new javax.swing.GroupLayout(jobsFace);
	        jobsFace.setLayout(jobsFaceLayout);
	        jobsFaceLayout.setHorizontalGroup(
	            jobsFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jobsFaceLayout.createSequentialGroup()
	                .addGroup(jobsFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(jobsFaceLayout.createSequentialGroup()
	                        .addContainerGap()
	                        .addComponent(jobslbl, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE))
	                    .addGroup(jobsFaceLayout.createSequentialGroup()
	                        .addGap(51, 51, 51)
	                        .addComponent(jobstxt, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)))
	                .addContainerGap())
	        );
	        jobsFaceLayout.setVerticalGroup(
	            jobsFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jobsFaceLayout.createSequentialGroup()
	                .addGap(27, 27, 27)
	                .addComponent(jobslbl, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(jobstxt, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(42, Short.MAX_VALUE))
	        );

	        masterUI.addTab("<html><body marginwidth=28 marginheight=4>Jobs</body></html>", jobsFace);

	        paginglbl.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
	        paginglbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	        paginglbl.setText("Turn on Paging?");

	        pagingchk.setSelected(true);
	        pagingchk.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

	        javax.swing.GroupLayout pagingFaceLayout = new javax.swing.GroupLayout(pagingFace);
	        pagingFace.setLayout(pagingFaceLayout);
	        pagingFaceLayout.setHorizontalGroup(
	            pagingFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(pagingFaceLayout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(pagingFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(pagingchk, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
	                    .addComponent(paginglbl, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE))
	                .addContainerGap())
	        );
	        pagingFaceLayout.setVerticalGroup(
	            pagingFaceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(pagingFaceLayout.createSequentialGroup()
	                .addGap(18, 18, 18)
	                .addComponent(paginglbl, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(pagingchk)
	                .addContainerGap(49, Short.MAX_VALUE))
	        );

	        masterUI.addTab("<html><body marginwidth=25 marginheight=4>Paging</body></html>", pagingFace);

	        jScrollPane1.setAutoscrolls(true);

	        OSTextArea.setColumns(20);
	        OSTextArea.setEditable(false);
	        OSTextArea.setRows(5);
	        OSTextArea.setText("WiMi messages:");
	        jScrollPane1.setViewportView(OSTextArea);

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(masterUI, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
	            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addComponent(masterUI, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
	        );

	        masterUI.getAccessibleContext().setAccessibleName("menubar");
	        
	        catchSystemText(); // comment out to restore print functionality

	        pack();
	    }// </editor-fold>

	    private void runButtonActionPerformed(java.awt.event.ActionEvent evt) throws NumberFormatException, IOException {                                          
	    	Driver.runOS(cpunumspin.getValue().toString(), getButtonPressed(),jobstxt.getText(), pagingchk.isSelected());
	    }//GEN-LAST:event_runButtonActionPerformed
	    
	    private String getButtonPressed(){
	    	
	    	if(FCFSbutton.isSelected()){
	    		return FCFSbutton.getText();
	    	}
	    	else if(prioritybutton.isSelected()){
	    		return prioritybutton.getText();
	    	}
	    	else if (SJFbutton.isSelected()){
	    		return SJFbutton.getText();
	    	}
	    	return "No Button Selected";
	    }
		
	    // Captures all System.out calls and displays them inside the GUI
		private void catchSystemText(){
			PrintStream aPrintStream  =
			       new PrintStream(
			         new FilteredStream(
			           new ByteArrayOutputStream()));
			
			System.setOut(aPrintStream); // catches System.out messages
	        System.setErr(aPrintStream); // catches error messages
		}
		
		class FilteredStream extends FilterOutputStream {
	        public FilteredStream(OutputStream aStream) {
	            super(aStream);
	          }
	        public void write(byte b[]) throws IOException {
	            String aString = new String(b);
	            OSTextArea.append(aString);
	        }

	        public void write(byte b[], int off, int len) throws IOException {
	            String aString = new String(b , off , len);
	            OSTextArea.append(aString);
	        }
	    }                                      



	    // Variables declaration - do not modify
	    private javax.swing.JRadioButton FCFSbutton;
	    private javax.swing.JTextArea OSTextArea;
	    private javax.swing.JRadioButton SJFbutton;
	    private javax.swing.ButtonGroup buttonGroup1;
	    private javax.swing.JSpinner cpunumspin;
	    private javax.swing.JPanel homeFace;
	    private javax.swing.JLabel homelbl;
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JPanel jobsFace;
	    private javax.swing.JLabel jobslbl;
	    private javax.swing.JTextField jobstxt;
	    private javax.swing.JTabbedPane masterUI;
	    private javax.swing.JLabel numCPUlbl;
	    private javax.swing.JPanel pagingFace;
	    private javax.swing.JCheckBox pagingchk;
	    private javax.swing.JLabel paginglbl;
	    private javax.swing.JRadioButton prioritybutton;
	    private javax.swing.JButton runButton;
	    private javax.swing.JPanel scheduleFace;
	    private javax.swing.JLabel schedulelbl;
	    // End of variables declaration



	
}
